<template>
  <bar :totalQuestions="items.length" />
  <div>
    <div style="height: 1em"></div>
    <span>章节编号:</span>
    <el-input v-model="chapter" style="width: 70%">
    </el-input>
    <div style="height: 1em"></div>
    <span>章节名称:</span>
    <el-input v-model="name" style="width: 70%">
    </el-input>
  </div>
  <div class="homework-list-container">
    <div class="homework-list-item" v-for="(item, index) in items" :key="index">
      <div class="item-content">
        <h2 class="item-title">  {{index + 1}} . </h2>
        <v-md-preview :text="item.desc" style="padding: 0"></v-md-preview>
      </div>
      <el-radio-group class="homework-item">
        <el-radio value="a" > A. {{ item.A }} </el-radio>
        <el-radio value="b" > B. {{ item.B }} </el-radio>
        <el-radio value="c" > C. {{ item.C }} </el-radio>
        <el-radio value="d" > D. {{ item.D }} </el-radio>
      </el-radio-group>
      答案： {{ item.ans }}
    </div>

    <div class="new-homework-buttons">
      <button class="submit-button" @click="create">
        新建题目
      </button>
      <button class="submit-button" @click="publish">
        发布
      </button>
    </div>
  </div>
  <div style="display: flex; justify-content: center;">
    <Sidebar />
  </div>

  <el-dialog v-model="dialogFormVisible" title="请输入题目信息" width="90%">
    <div>
      <el-radio-group v-model="radio1" size="medium">
        <el-radio-button label="单选" value="1" />
        <el-radio-button label="多选" value="2" />
        <el-radio-button label="判断" value="3" />
        <el-radio-button label="填空" value="4" />
        <el-radio-button label="简答" value="5" />
      </el-radio-group>
    </div>
    <div class="ques">
      <div style="display: flex">
        <p>题目:</p>
        <el-input v-model="desc" style="width: 70%" type="textarea">
        </el-input>
      </div>
      <div style="display: flex" v-show="radio1 <= 2">
        <p>A:</p> &nbsp &nbsp &nbsp
        <el-input v-model="A" style="width: 70%" type="textarea">
        </el-input>
      </div>
      <div style="display: flex" v-show="radio1 <= 2">
        <p>B:</p> &nbsp &nbsp &nbsp
        <el-input v-model="B" style="width: 70%" type="textarea">
        </el-input>
      </div>
      <div style="display: flex" v-show="radio1 <= 2">
        <p>C:</p> &nbsp &nbsp &nbsp
        <el-input v-model="C" style="width: 70%" type="textarea">
        </el-input>
      </div>
      <div style="display: flex" v-show="radio1 <= 2">
        <p>D:</p> &nbsp &nbsp &nbsp
        <el-input v-model="D" style="width: 70%" type="textarea">
        </el-input>
      </div>
      <div style="display: flex">
        <p>答案:</p>
        <el-input v-model="ans" style="width: 70%" type="textarea">
        </el-input>
      </div>
    </div>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="confirmCreate">
          确认添加
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import Bar from './components/bar5.vue';
import Sidebar from '@/components/Sidebar.vue';
import { ElMessage } from 'element-plus'

export default {
  components: {
    Bar,
    Sidebar,
  },
  data() {
    return {
      desc : '',
      A : '',
      B: '',
      C: '',
      D: '',
      ans: '',
      chapter: '',
      name: '',
      dialogFormVisible : 0,
      radio1 : 0,
      items: []
    }
  },
  methods: {
    create() {
      this.dialogFormVisible = ! this.dialogFormVisible
    },
    confirmCreate() {
      this.items.push({
        desc: this.desc,
        A : this.A,
        B : this.B,
        C : this.C,
        D : this.D,
        ans : this.ans
      })
      this.create()
      this.A = ''
      this.B = ''
      this.C = ''
      this.D = ''
      this.desc = ''
      this.ans = ''
    },
    publish() {
      ElMessage({
        type: 'success',
        message: '发布成功！'
      })
    },
  }
}
</script>

<style scoped>

.ques {
  display: flex;
  flex-direction: column;
  margin-top: 5%;
  gap: 1em;
}

.homework-list-container {
  height: 68%;
  display: flex;
  flex-direction: column;
  margin-top: 20px;
  overflow: scroll;
}

.homework-list-item {
  text-align: left;
  padding: 16px;
  background: #ffffff;
  border-radius: 8px;
  box-shadow: 0 0 4px rgba(0, 0, 0, 0.1);
  margin-bottom: 8px;
}

.item-content {
  display: flex;
  flex-direction: column;
}

.item-title {
  font-size: 18px;
  font-weight: bold;
  margin: 0;
}

.homework-item {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  font-size: 16px;
  color: #666;
}

.new-homework-buttons {
  display: flex;
  justify-content: center;
  gap: 10%
}

.item-select {
  background-color: transparent;
  border: none;
  cursor: pointer;
}

.circle {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  border: 1px solid #ccc;
  display: inline-block;
  margin-right: 10px;
}

.selected {
  background-color: #41A2FF;
}

.submit-button {
  margin-top: 20px;
  background-color: #41A2FF;
  color: aliceblue;
  padding: 12px 20px;
  font-size: 16px;
  border: none;
  border-radius: 5px;
  cursor: pointer;
}
</style>
